<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="输入姓名、用户名、手机" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <!-- <el-select v-model="listQuery.type" placeholder="Type" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select> -->

      <span class="filter-item">
        <el-button v-waves type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>

        <router-link :to="'/member/inspection/create'">
          <el-button style="margin-left: 10px;" type="primary" icon="el-icon-edit">
            新建
          </el-button>
        </router-link>
      </span>

    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="title" sortable="custom" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{row.title}}</span>
        </template>
      </el-table-column>

      <el-table-column label="联系人" prop="realname" sortable="custom" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{row.realname}}</span>
        </template>
      </el-table-column>
      <el-table-column label="电话" prop="mobile" sortable="custom" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{row.mobile}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" prop="username" sortable="custom" align="center" width="150">
        <template slot-scope="{row}">
          <span>{{row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center">
        <template slot-scope="{row}">
          <span>{{row.address}}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" prop="created_at" sortable="custom" align="center">
        <template slot-scope="{row}">
          <span>{{row.created_at}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">

          <router-link :to="'/member/inspection/edit/'+row.id">
            <el-button style="margin-left: 10px;" type="primary" size="mini" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>

          <el-popconfirm v-if="row.status!='deleted'" title="确定删除吗？" @confirm="handleDelete(row,$index)">
            <el-button slot="reference" style="margin-left: 10px;" size="mini" type="danger">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>
<style >
.filter-container .filter-item .el-input__inner {
  border-radius: 0;
}
.filter-container .filter-item:first-child > .el-input__inner {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
.filter-container span > button:first-of-type {
  border-radius: 0;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
/* .filter-container span.filter-item:first-child.el-button--medium {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
} */
</style>
<script>
import { fetchList, deleteInspection } from '@/api/member/inspection'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 2,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },

      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      // 排序筛选
      const { prop, order } = data

      if (order === 'ascending') {
        this.listQuery.sort = '+' + prop
      } else {
        this.listQuery.sort = '-' + prop
      }
      this.handleFilter()
    },


    handleDelete(row, index) {
      // 删除
      deleteInspection(row).then(() => {
        this.$notify({
          title: '提醒',
          message: '删除成功！',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      })


    },

  }
}
</script>
